﻿//using MySql.Data.MySqlClient;
using System;
using System.IO;
using System.Xml.Linq;


namespace BusinessDll
{
    public class DBtools
    {
#if implment
        #region 
        /// <summary>
        /// 数据库常用方法
        /// </summary>
        public DBtools()
        {
            /**
            server = generalTools.getConfigByName("GENERAL", "DATABASESERVER");
            database = generalTools.getConfigByName("GENERAL", "DATABASENAME");
            uid = generalTools.getConfigByName("GENERAL", "DATABASEUSER");
            password = generalTools.getConfigByName("GENERAL", "DATABASEPWD");
            **/
            MySqlSetContents();
        }

        ~DBtools()
        {

        }
        public MySqlConnection connection;
        private string server { set; get; }
        private string database { set; get; }
        private string uid { set; get; }
        private string password { set; get; }
        /// <summary>
        /// 初始化MySql连接
        /// </summary>
        private void MySqlSetContents()
        {
            string connectionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";Charset=utf8";
System.Diagnostics.Debug.WriteLine("db OUTPUT: "+connectionString);
            connection = new MySqlConnection(connectionString);
        }

        public bool OpenConnection()
        {
            try
            {
                connection.Open();
                return true;
            }
            catch (MySqlException ex)
            {
            	System.Diagnostics.Debug.WriteLine(ex.StackTrace);
                switch (ex.Number)
                {
                    case 0:
                        //WriteFileLog("数据库连接成功！");
                        break;

                    case 1045:
                        //WriteFileLog("数据库连接失败，错误代码：1045！");
                        break;
                }
                return false;
            }
        }
        public bool CloseConnection()
        {
            try
            {
                connection.Close();
                return true;
            }
            catch (MySqlException ex)
            {
                switch (ex.Number)
                {
                    case 0:

                        break;

                    case 1045:
                        break;
                }
                return false;
            }
        }

        public int Insert(string query)
        {

            if (OpenConnection() == true)
            {
                MySqlCommand cmd = new MySqlCommand(query, connection);

                try
                {
                    cmd.ExecuteNonQuery();
                }
                catch (MySqlException ex)
                {
                    WriteFileLog("新增数据失败失败，错误代码：" + ex.ToString() + "执行语句：" + query);
                    CloseConnection();
                    return -1;
                }

                CloseConnection();
            }
            return 0;
        }

        public void Update(string query)
        {

            if (OpenConnection() == true)
            {
                MySqlCommand cmd = new MySqlCommand
                {
                    CommandText = query,
                    Connection = connection
                };
                cmd.ExecuteNonQuery();
                CloseConnection();
            }
        }

        public void Delete(string query)
        {
            if (OpenConnection() == true)
            {
                MySqlCommand cmd = new MySqlCommand(query, connection);
                cmd.ExecuteNonQuery();
                CloseConnection();
            }
        }

        #endregion

        public void WriteFileLog(string msg)
        {
            if (!Directory.Exists("\\logs"))
            {
                Directory.CreateDirectory("\\logs");
            }
            string strFilePath = "\\logs\\DB_" + DateTime.Now.ToString("yyyyMMdd") + ".log";
            System.IO.FileStream fs = new System.IO.FileStream(strFilePath, System.IO.FileMode.Append);
            System.IO.StreamWriter sw = new System.IO.StreamWriter(fs, System.Text.Encoding.Default);
            sw.WriteLine("[" + DateTime.Now.ToString("yyyyMMddHHmmss") + "] MSG:" + msg);
            sw.Close();
            fs.Close();
        }
#endif
    }
}
